#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/18 15:47
    Desc  :
--------------------------------------
"""
import time
import common.libs.FakerUtils as fakers
from apitestUtils.requestsUtils import RequestsUtils
from loguru import logger

from common.libs.ExcelUtils import ExcelUtils


class BaseRespinse(RequestsUtils):

    def responseDict(self):
        return {
            'apiname': self.apiName,
            'apiParam': self.apiParam,
            'apiPath': self.apiPath,
            'extract': self.extract,
            'assert': {self.assertPatam: self.assertValue},
            'runCount': self.runCount,
        }


class RunCaseUtils:
    """
    执行测试用例
    """

    def __init__(self, excelData: list):
        # 读出的excel数据
        self.excelData = excelData
        # 开始时间
        self.begin_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.startTime = time.time()
        # 返回结果列表
        self.responseList = []
        # 用例通过数量
        self.testPass = 0
        # 用例总数
        self.testAll = 0
        # 用例失败数量
        self.testFail = 0
        # 用例错误数量
        self.testError = 0
        # 用例跳过数量
        self.testSkip = 0

        # 总耗时
        self.totalTime = 0

        self.summary = [
            {
                "value": self.testAll,
                "name": "用例总数"
            },
            {
                "value": self.testPass,
                "name": "用例通过"
            },
            {
                "value": self.testFail,
                "name": "用例失败"
            },
            {
                "value": self.testSkip,
                "name": "用例跳过"
            },
            {
                "value": self.begin_time,
                "name": "开始时间"
            },
            {
                "value": f'{self.totalTime}s',
                "name": "耗时"
            }
        ]

    def caseRun(self):
        """
        执行
        :return:
        """
        caseResposne = []
        for i in self.excelData:
            requestsutils = BaseRespinse(i)
            if requestsutils.isRun == 0:
                if isinstance(requestsutils.runCount, float):
                    for j in range(int(requestsutils.runCount)):
                        self.testAll += 1
                        logger.info(
                            f"--------------------------第{j + 1}次--请求开始--{requestsutils.apiName}--------------------------")
                        response = requestsutils.request()
                        caseResposne.append(response)
                        isPass = requestsutils.isPass
                        if isPass == 0:
                            self.testPass += 1
                        elif isPass == 1:
                            self.testFail += 1
                        elif isPass == 2:
                            self.testError += 1
                    response = requestsutils.responseDict()
                    response['responses_list'] = caseResposne
                    self.responseList.append(response)
                    # 重置列表
                    caseResposne = []
                else:
                    self.responseList.append(requestsutils.skip_response())
                    self.testSkip += 1
                    self.testAll += 1
        self.totalTime = round(time.time() - self.startTime, 2)

        return self.resultDict()

    def resultDict(self):

        return {
            'responseList': self.responseList,
            'testAll': self.testAll,
            'testPass': self.testPass,
            'testFail': self.testFail,
            'testError': self.testError,
            'testSkip': self.testSkip,
            'summary': self.summary,
        }
